package com.wenhuitec.android.tang.ui.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhkj.printlib.PrintStart;
import com.dhkj.web.bean.OrderProductModel;
import com.dhkj.web.bean.ProductModel;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.entity.Foods;
import com.wenhuitec.android.tang.ui.adapter.FoodsAdapter;
import com.wenhuitec.android.tang.ui.adapter.OrderAdapter;
import com.yzy.voice.VoicePlay;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class OrdersDialog extends AppCompatActivity {
    private Context mContext;
    private RecyclerView recyclerView;
    private String jsonStr;
    private TextView orderTv;
    FoodsAdapter foodsAdapter;
    private TextView amountCountTv;
    private TextView amountPriceTv;

    public static void startAct(Context context, String json) {
        Intent intent = new Intent(context, OrdersDialog.class);
        intent.putExtra("json", json);
        context.startActivity(intent);
    }

    private OrderProductModel productModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_orders);
        recyclerView = findViewById(R.id.foodsRv);
        orderTv = findViewById(R.id.orderTv);
        amountCountTv = findViewById(R.id.amountCountTv);
        amountPriceTv = findViewById(R.id.amountPriceTv);

        mContext = this;
        findViewById(R.id.printBut).setOnClickListener(v -> {
            PrintStart.with(mContext).printPP();
            finish();
        });
        if (getIntent() != null) {
            jsonStr = getIntent().getStringExtra("json");
            productModel = JSONObject.parseObject(jsonStr, OrderProductModel.class);
            System.out.println(productModel.toString());
        }
        foodsAdapter = new FoodsAdapter(mContext, new ArrayList<>());
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(foodsAdapter);

        if (productModel != null) {
            orderTv.setText("单号" + productModel.getDh());
            List<Foods> foodsList = new ArrayList<>();
            BigDecimal price = new BigDecimal(0);
            BigDecimal count = new BigDecimal(0);
            for (OrderProductModel.Product goo : productModel.getProductList()) {
                Foods foods = new Foods();
                foods.setName(goo.getDeviceName());
                foods.setNumber(goo.getNumber());
                foods.setPrice(new BigDecimal(goo.getNumber()).multiply(new BigDecimal(goo.getPrice())).toPlainString());
                foodsList.add(foods);
                price = price.add(new BigDecimal(foods.getPrice()));
                count = count.add(new BigDecimal(foods.getNumber()));
            }
            amountCountTv.setText("X" + count.stripTrailingZeros().toPlainString());
            amountPriceTv.setText("￥" + price.stripTrailingZeros().toPlainString());
            foodsAdapter.setFoodsList(foodsList);
        }
    }
}
